/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Class to copy, mopve file from one directory to other
*  Name: MoveFile.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.batch.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Used to copy, move a file from one directory to another
 */

public class MoveFile
{
	private static int bufferSize = 4 * 1024;
	private static byte[] buffer = null;
	private static long time;
	private static InputStream in = null;
	private static OutputStream out = null;
	private static int bytesRead;

	public static void copy(File source, File dest) throws IOException
	{
		in = new FileInputStream(source);
		out = new FileOutputStream(dest);
		time = System.currentTimeMillis();	
		buffer = new byte[bufferSize];
		while ((bytesRead = in.read(buffer)) >= 0) 
		{
			out.write(buffer, 0, bytesRead);
		}
		out.close();
		in.close();
		time = System.currentTimeMillis() - time;
		System.out.println("Second(s): " + (time/1000L));
	}

	public static void move(File source, File dest) throws IOException
	{
		in = new FileInputStream(source);
		out = new FileOutputStream(dest);
		time = System.currentTimeMillis();	
		buffer = new byte[bufferSize];
		while ((bytesRead = in.read(buffer)) >= 0) 
		{
			out.write(buffer, 0, bytesRead);
		}
		out.close();
		in.close();
		source.delete();
		time = System.currentTimeMillis() - time;
		System.out.println("Second(s): " + (time/1000L));
	}

	public static void main(String args[])
	{
		try
		{
			copy(new File("E:\\content\\pecats\\Log\\access.log"), new File("E:\\content\\pecats\\access.log"));
		}
		catch(IOException ie)
		{
		}
	}

}